<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份证号" prop="idNum">
        <el-input
          v-model="queryParams.idNum"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="expertName">
        <el-input
          v-model="queryParams.expertName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="expertName">
        <el-input
          v-model="queryParams.sex"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业类型" prop="expertType">
        <el-select v-model="queryParams.expertType" placeholder="请选择专业类型" clearable>
          <el-option
            v-for="dict in dict.type.major_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="isBusy">
        <el-select v-model="queryParams.isBusy" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.expert_is_busy"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保密级别" prop="secretGrade">
        <el-select v-model="queryParams.secretGrade" placeholder="请选择保密级别" clearable>
          <el-option
            v-for="dict in dict.type.secret_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="移动电话" prop="mobileTel">
        <el-input
          v-model="queryParams.mobileTel"
          placeholder="请输入移动电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="办公电话" prop="officeTel">
        <el-input
          v-model="queryParams.officeTel"
          placeholder="请输入办公电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电子邮件" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入电子邮件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rm:expert:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rm:expert:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rm:expert:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rm:expert:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expertList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="身份证" align="center" prop="idNum" width="180"/>
      <el-table-column label="姓名" align="center" prop="expertName" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="籍贯" align="center" prop="birthPlace" />
      <el-table-column label="户籍" align="center" prop="regResidence" />
      <el-table-column label="出生日期" align="center" prop="birthday" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="家庭住址" align="center" prop="homeAddress" />
      <el-table-column label="健康状况" align="center" prop="health" />
      <el-table-column label="专业类型" align="center" prop="expertType" width="100px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.major_type" :value="scope.row.expertType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isBusy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.expert_is_busy" :value="scope.row.isBusy"/>
        </template>
      </el-table-column>
      <el-table-column label="工作单位" align="center" prop="department" />
      <el-table-column label="政治面貌" align="center" prop="party" />
      <el-table-column label="职称" align="center" prop="title" />
      <el-table-column label="行政职务" align="center" prop="headShip" />
      <el-table-column label="保密级别" align="center" prop="secretGrade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.secret_type" :value="scope.row.secretGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="移动电话" align="center" prop="mobileTel" />
      <el-table-column label="办公电话" align="center" prop="officeTel" />
      <el-table-column label="电子邮件" align="center" prop="email" />
      <el-table-column label="最高学历" align="center" prop="qualification" />
      <el-table-column label="特长描述" align="center" prop="speciality" />
      <el-table-column label="工作经历" align="center" prop="experience" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="操作" align="center" width="180px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rm:expert:edit']"
            plain
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rm:expert:remove']"
            plain
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专家信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="身份证号" prop="idNum">
          <el-input v-model="form.idNum" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="姓名" prop="expertName">
          <el-input v-model="form.expertName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="姓别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="籍贯" prop="birthPlace">
          <el-input v-model="form.birthPlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="户籍" prop="regResidence">
          <el-input v-model="form.regResidence" placeholder="请输入户籍" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="健康状况" prop="health">
          <el-input v-model="form.health" placeholder="请输入健康状况" />
        </el-form-item>
        <el-form-item label="专业类型" prop="expertType">
          <el-select v-model="form.expertType" placeholder="请选择专业类型">
            <el-option
              v-for="dict in dict.type.major_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="isBusy">
          <el-radio-group v-model="form.isBusy">
            <el-radio
              v-for="dict in dict.type.expert_is_busy"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="工作单位" prop="department">
          <el-input v-model="form.department" placeholder="请输入工作单位" />
        </el-form-item>
        <el-form-item label="政治面貌" prop="party">
          <el-input v-model="form.party" placeholder="请输入政治面貌" />
        </el-form-item>
        <el-form-item label="职称" prop="title">
          <el-input v-model="form.title" placeholder="请输入职称" />
        </el-form-item>
        <el-form-item label="行政职务" prop="headShip">
          <el-input v-model="form.headShip" placeholder="请输入行政职务" />
        </el-form-item>
        <el-form-item label="保密级别" prop="secretGrade">
          <el-select v-model="form.secretGrade" placeholder="请选择保密级别">
            <el-option
              v-for="dict in dict.type.secret_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="移动电话" prop="mobileTel">
          <el-input v-model="form.mobileTel" placeholder="请输入移动电话" />
        </el-form-item>
        <el-form-item label="办公电话" prop="officeTel">
          <el-input v-model="form.officeTel" placeholder="请输入办公电话" />
        </el-form-item>
        <el-form-item label="电子邮件" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮件" />
        </el-form-item>
        <el-form-item label="最高学历" prop="qualification">
          <el-input v-model="form.qualification" placeholder="请输入最高学历" />
        </el-form-item>
        <el-form-item label="特长描述" prop="speciality">
          <el-input v-model="form.speciality" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工作经历" prop="experience">
          <el-input v-model="form.experience" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExpert, getExpert, delExpert, addExpert, updateExpert } from "@/api/resourcemanagement/expert";

export default {
  name: "Expert",
  dicts: ['major_type', 'expert_is_busy', 'secret_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专家信息表格数据
      expertList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idNum: null,
        expertName: null,
        expertType: null,
        isBusy: null,
        secretGrade: null,
        mobileTel: null,
        officeTel: null,
        email: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        idNum: [
          { required: true, message: "身份证号码不能为空", trigger: "blur" }
        ],
        expertName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        nation: [
          { required: true, message: "民族不能为空", trigger: "blur" }
        ],
        birthPlace: [
          { required: true, message: "籍贯不能为空", trigger: "blur" }
        ],
        regResidence: [
          { required: true, message: "户籍不能为空", trigger: "blur" }
        ],
        birthday: [
          { required: true, message: "出生日期不能为空", trigger: "blur" }
        ],
        homeAddress: [
          { required: true, message: "家庭住址不能为空", trigger: "blur" }
        ],
        expertType: [
          { required: true, message: "专业类型不能为空", trigger: "change" }
        ],
        isBusy: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        department: [
          { required: true, message: "工作单位不能为空", trigger: "blur" }
        ],
        party: [
          { required: true, message: "政治面貌不能为空", trigger: "blur" }
        ],
        secretGrade: [
          { required: true, message: "保密级别不能为空", trigger: "change" }
        ],
        mobileTel: [
          { required: true, message: "移动电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询专家信息列表 */
    getList() {
      this.loading = true;
      listExpert(this.queryParams).then(response => {
        this.expertList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        idNum: null,
        expertName: null,
        sex: null,
        nation: null,
        birthPlace: null,
        regResidence: null,
        birthday: null,
        homeAddress: null,
        health: null,
        expertType: null,
        isBusy: null,
        department: null,
        party: null,
        title: null,
        headShip: null,
        secretGrade: null,
        major: null,
        mobileTel: null,
        officeTel: null,
        email: null,
        qualification: null,
        speciality: null,
        experience: null,
        createTime: null,
        updateTime: null,
        notes: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专家信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExpert(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改专家信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExpert(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpert(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除专家信息编号为"' + ids + '"的数据项？').then(function() {
        return delExpert(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rm/expert/export', {
        ...this.queryParams
      }, `expert_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
